﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace YAPIExample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
        protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                YAPI.Data.appName = "MTH Youtube";
                YAPI.Data.playTime = new TimeSpan();

                if (YAPI.Data.jsonVideoPage == "")
                {
                    itemsCollection.ItemsSource = await YAPI.Methods.videosOfChannel("maitrunghieuit", 1, false, null);
                    pro.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch
            {
                MessageBox.Show("Check internet connection!");
            }
        }

        private async void prePage_Tap(object sender, GestureEventArgs e)
        {
            pro.Visibility = System.Windows.Visibility.Visible;
            itemsCollection.Visibility = System.Windows.Visibility.Collapsed;

            List<YAPI.VideoItem> li = YAPI.Data.videoList;
            itemsCollection.ItemsSource = ((await YAPI.Methods.videosOfPreviousPage()).Count != 0) ? YAPI.Data.videoList : li;

            nextPage.Visibility = System.Windows.Visibility.Visible;
            if (YAPI.Data.videoList.Count == 0)
            {
                YAPI.Data.currentVideoPage++;
                prePage.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (YAPI.Data.currentVideoPage == 1)
            {
                prePage.Visibility = System.Windows.Visibility.Collapsed;
            }

            itemsCollection.Visibility = System.Windows.Visibility.Visible;
            pro.Visibility = System.Windows.Visibility.Collapsed;
            itemsCollection.UpdateLayout();
            itemsCollection.ScrollIntoView(itemsCollection.Items[0]);
        }

        private async void nextPage_Tap(object sender, GestureEventArgs e)
        {
            pro.Visibility = System.Windows.Visibility.Visible;
            itemsCollection.Visibility = System.Windows.Visibility.Collapsed;

            List<YAPI.VideoItem> li = YAPI.Data.videoList;
            itemsCollection.ItemsSource = ((await YAPI.Methods.videosOfNextPage()).Count != 0) ? YAPI.Data.videoList : li;

            prePage.Visibility = System.Windows.Visibility.Visible;
            if (YAPI.Data.videoList.Count == 0)
            {
                YAPI.Data.currentVideoPage--;
                nextPage.Visibility = System.Windows.Visibility.Collapsed;
                MessageBox.Show("No video");
            }

            itemsCollection.Visibility = System.Windows.Visibility.Visible;
            pro.Visibility = System.Windows.Visibility.Collapsed;
            itemsCollection.UpdateLayout();
            itemsCollection.ScrollIntoView(itemsCollection.Items[0]);
        }

        private void HD_Tap_1(object sender, GestureEventArgs e)
        {
            YAPI.Data.HDvideo = (bool)HD.IsChecked;
        }

        private void video_Tap_1(object sender, GestureEventArgs e)
        {
            YAPI.Data.selectedVideoItem = (sender as Grid).DataContext as YAPI.VideoItem;
            NavigationService.Navigate(new Uri("/Play.xaml", UriKind.Relative));
        }

        private void fb_Tap_1(object sender, GestureEventArgs e)
        {
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri("http://www.facebook.com/maitrunghieuit");
            webBrowserTask.Show();
        }

        private void about_Tap(object sender, GestureEventArgs e)
        {
            if (about.Visibility == System.Windows.Visibility.Visible)
                about.Visibility = System.Windows.Visibility.Collapsed;
            else
                about.Visibility = System.Windows.Visibility.Visible;
        }
    }
}